﻿Ext.define('app.prm.document.MultipleUploadFile',
    {
        extend: 'Ext.window.Window',
        title: '上传文档',
        autoScroll: true,
        modal: true,
        width: 600,
        autoHeight: true,
        layout: 'fit',
        ProjectId: 0,
        TypeId: 0,
        stepname: '',
        requires: ['app.ux.MultiSelect'],
        initComponent: function () {
            var me = this;
            me.items = [this.getUploadForm()];
            me.buttons = [
                { text: '保存', scope: this, handler: me.onSave },
                {
                    text: '关闭', handler: function () {
                        me.close();
                    }
                }
            ]
            me.callParent();
        },
        getUploadForm: function () {
            var me = this;
            if (!this.uploadForm) {
                this.uploadForm = new Ext.form.Panel({
                    defaultType: 'textfield',
                    border: false,
                    defaults: { anchor: '100%' },
                    fieldDefaults: {
                        labelWidth: 90,
                        labelAlign: "right",
                        flex: 1,
                        margin: 6
                    },
                    items: [
                        {
                            xtype: 'filefield', fieldLabel: '文档附件', name: 'ProjectFile', buttonText: '浏览'
                            , supportMultFn: function ($this) {
                                //2.1 为input添加支持多文件选择属性
                                var typeArray = ["pdf", "doc", "docx", "flv-application/octet-stream"];
                                var fileDom = $this.getEl().down('input[type=file]');
                                fileDom.dom.setAttribute("multiple", "multiple");
                                fileDom.dom.setAttribute("accept", typeArray.join(","));
                            },
                            listeners: {
                                change: function (fld, value) {
                                    var fileDom = this.getEl().down('input[type=file]');
                                    var files = fileDom.dom.files;
                                    var fileArr = [];
                                    for (var i = 0; i < files.length; i++) {
                                        fileArr.push((i + 1) + "、" + files[i].name + ",大小:" + files[i].size / 1024 + "KB<br/>");
                                    }
                                    Ext.getCmp('fileInfo').update(fileArr.join("\n\n"));
                                    //files[0].name / files[0].type / files[0].size 
                                    //2.4 选择完后input会还原单选，所以还需要再次重写
                                    this.supportMultFn(this);
                                },
                                afterrender: function (cmp) {
                                    this.supportMultFn(this);
                                }
                            }
                        },
                        {
                            xtype: 'panel',
                            border: false,
                            maxHeight: 300,
                            autoScroll: true,
                            id: "fileInfo",
                            html: ''
                        },
                        {
                            name: 'StepName',
                            value: this.stepname,
                            fieldLabel: '所属步骤'
                        }
                    ]
                });
            }
            return this.uploadForm;
        },
        onSave: function () {
            var me = this;
            me.uploadForm.getForm().submit({
                url: '/prm/document/UploadDocuments',
                params: { ProjectId: me.ProjectId, TypeId: me.TypeId },

                success: function (form, response) {
                    Ext.Msg.alert("系统提示", '上传成功');
                    me.fireEvent("savesuccess");
                },
                failure: function () {
                    Ext.Msg.alert("系统提示", '上传失败');
                }
            });
        }
    });
